package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class WmSensitiveImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CacheService cacheService;

    /**
     * 条件分页查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listAllSensitive(SensitiveDto dto) {
        //1.入参校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.分页参数检查
        dto.checkParam();

        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        //3,模糊敏感词查询
        if (StringUtils.isNoneBlank(dto.getName())) {
            lqw.like(WmSensitive::getSensitives, dto.getName());
        }
        //倒序展示
        lqw.orderByDesc(WmSensitive::getCreatedTime);

        IPage iPage = new Page(dto.getPage(), dto.getSize());
        page(iPage, lqw);

        //4.封装数据,返回
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());

        return pageResponseResult;
    }

    /**
     * 新增敏感词
     * 同时清理redis中的敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive dto) {
        //1.入参校验
        if (dto == null || StringUtils.isBlank(dto.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.校验是否重复  :已存在的敏感词则不能保存
        int count = count(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词[" + dto.getSensitives() + "]已存在!");
        }
        //3.补全字段
        dto.setCreatedTime(new Date());
        //4.插入数据库
        save(dto);

        //清理redis中的敏感词缓存
        clearRedisSensitive();
        //5.返回数据响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive dto) {
        //1.入参校验
        if (dto == null || dto.getId() == null || StringUtils.isBlank(dto.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断新的敏感词是否已存在
        int count = count(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词[" + dto.getSensitives() + "]已存在!");
        }
        //3.修改数据
        updateById(dto);

        //清理redis中的敏感词缓存
        clearRedisSensitive();

        //4.返回数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitive(Integer id) {
        //1.入参校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.根据id查询是否存在敏感词
        WmSensitive dbSensitive = getById(id);
        if (dbSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "不存在该敏感词!");
        }

        //清理redis中的敏感词缓存
        clearRedisSensitive();

        //3.删除
        removeById(id);

        //4.返回数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 清理redis中的敏感词数据
     * key : SensitiveKeys
     */
    private void clearRedisSensitive() {
        cacheService.delete("SensitiveKeys");
        System.out.println("redis敏感词库删除.ok..");
    }
}
